<?php
namespace app\common\model;
class Member extends Common {
	protected $insert = ['regtime', 'avatar'];
	protected function getSysManageAttr($value, $data) {
		$SysManage = '<a href="' . url("chongzhi", ['items' => $data['id']]) . '">查看详细信息</a> |';
		if ($data['status'] == 1) {
			$SysManage .= ' <a class="cocoAjaxPost" href="Javascript:" confirm="确定要启用吗？" ajax-url="' . url("setstatus") . '" ajax-data="{  items:' . $data['id'] . ', setstatus:0}" style="color: green;">开启</a>';
		} else {
			$SysManage .= ' <a class="cocoAjaxPost" href="Javascript:" confirm="确定要锁定吗？" ajax-url="' . url("setstatus") . '" ajax-data="{  items:' . $data['id'] . ', setstatus:1}" style="color: red;" >锁定</a>';
		}
		$ispunish = model('Punish')->where('userid', $data['id'])->where('status', 1)->where('typeid', 1)->where('addtime + days * 24 * 3600', 'gt', SYS_TIME)->count();
		if ($ispunish) {
			$SysManage .= ' | <a class="cocoAjaxPost" href="Javascript:" confirm="确定取消该用户的惩罚吗？" ajax-url="' . url("punish/set_status") . '" ajax-data="{  id:' . $data['id'] . ', status:0}" style="color: green;" >取消惩罚</a>';
		} else {
			$SysManage .= ' | <a class="cocoAjaxPost" href="Javascript:" confirm="确定该用户因恶意接单要受到惩罚吗？" ajax-url="' . url("punish/add") . '" ajax-data="{  id:' . $data['id'] . '}" style="color: red;" >惩罚</a>';
		}
		$SysManage .= ' | <a class="cocoModal" href="' . url("setamount", ['id' => $data['id'], 'typeid' => 1]) . '" isForm fixat="1" layerwidth="500px" layerheight="400px">手动增减红豆</a>';
		$SysManage .= ' | <a class="cocoModal" href="' . url("setamount", ['id' => $data['id'], 'typeid' => 3]) . '" isForm fixat="1" layerwidth="500px" layerheight="400px">手动增减保障金</a>';
		return $SysManage;
	}
	protected function getLogintimeAttr($value) {
		return $value ? date("Y-m-d H:i:s", $value) : "从未登录过";
	}
	protected function getRegtimeAttr($value) {
		return $value ? date("Y-m-d H:i:s", $value) : "未知";
	}
	protected function getNicknameAttr($value, $data) {
		return $value ? $value : encrypt($data['phone']);
	}
	protected function getStatusnameAttr($value, $data) {
		if (empty($data)) {
			return false;
		}
		$status = [0 => '正常', 1 => '禁用'];
		return $status[$data['status']];
	}
	protected function getDatelineAttr($value) {
		return $value ? $value : '一个月';
	}
	protected function getBusinessnameAttr($value, $data) {
		$arr = [1 => '计算机', 2 => '财经'];
		return $arr[$data['business']];
	}
	protected function getAvatarAttr($value, $data) {
		$value = $value ? $value : '/upload/20171123/85fb39b735ea0559e3be7479e85e3225.png';
		if (strpos($value, "http://") !== false) {
			return $value;
		} else {
			return $value ? rtrim(APP_URL, "/") . $value : '';
		}
	}
	protected function getThumbAttr($value, $data) {
		if (strpos($value, "http://") !== false) {
			return $value;
		} else {
			return $value ? rtrim(APP_URL, "/") . $value : '';
		}
	}
	protected function getIdcardnameAttr($value, $data) {
		$arr = config("CARD_TYPE");
		return $data['idcardtype'] ? $arr[$data['idcardtype']] : '';
	}
	protected function getBirthdayAttr($value, $data) {
		return $value == '0000-00-00' ? date('Y-m-d', SYS_TIME) : $value;
	}
	protected function setRegtimeAttr($value) {
		return $value ? strtotime($value) : SYS_TIME;
	}
	protected function setGenderAttr($value) {
		return $value ? $value : '保密';
	}
	protected function setRecomAttr($value, $data) {

		return create_randomstr(8);
	}
	protected function setAvatarAttr($value, $data) {
		return isset($data['avatar']) && $data['avatar'] ? $data['avatar'] : '/upload/20171123/85fb39b735ea0559e3be7479e85e3225.png';
	}
	protected function setLogintimeAttr($value) {
		return $value ? strtotime($value) : SYS_TIME;
	}
	public function getList() {
		return ['id' => 1, 'nickname' => 'sadfasdf'];
	}
	protected function setPasswordAttr($value) {
		return md5($value);
	}
	public function get_member_avatar($id) {
		if (is_array($id)) {
			$uinfo = $id;
			$id = $uinfo['id'];
		}
		$avatar = $this->where('id', $id)->value('avatar');
		if ($avatar) {
			// $avatar = $avatar ? rtrim(ROOT_PATH,"/").UPLOAD_URL.$uinfo['avatar'] : IMG_PATH."nopic.png";
			$avatar = rtrim(APP_URL, "/") . $avatar;
		}
		return $avatar;
	}
	public function get_member_nickname($id) {
		if (is_array($id)) {
			$uinfo = $id;
		} else {
			$uinfo = $this->where('id', $id)->field("username,nickname,id")->find();
		}
		if (!$uinfo['nickname']) {
			$uinfo['nickname'] = $uinfo['username'] ? $uinfo['username'] : $uinfo['id'];
		}
		return $uinfo['nickname'];
	}
	public function get_member_realname($id) {
		// if(is_array($id)){
		// 	$uinfo = $id;
		// 	$id = $uinfo['id'];
		// }
		// else{
		$uinfo = $this->where('id', $id)->field("username,realname,nickname,phone")->find();
		// }
		if (!$uinfo['realname']) {
			$uinfo['realname'] = $uinfo['nickname'] ? $uinfo['nickname'] : $uinfo['phone'];
		}
		return $uinfo['realname'];
	}
	public function save_data($data) {
		$result = $this->validate("Member.add")->allowField(true)->save($data);
		if ($result !== false) {
			$pk = $this->getPk();
			$id = $this->$pk;
			return $id;
		}
		return false;
	}
	public function login($phone, $password) {
		$iscount = $this->where("username|phone|email", $phone)->where("password", $password)->count();
		if ($iscount) {
			// $this->where("phone",$phone)->where("password",$password)->setField("logintime",SYS_TIME);
			// $this->where("phone",$phone)->where("password",$password)->setInc('loginnums');
			return $this->where("username|phone|email", $phone)->where("password", $password)->value("id");
		}
		return false;
	}
	public function get_data($data, $except = '', $field = '') {
		$data = parent::get_data($data, 'password, paypassword');
		return $data;
	}
	public function update_data($data) {
		if ($data['password'] || $data['repassword']) {
			$this->validate('Member.edit_pwd');
		} else {
			unset($data['password']);
			$this->validate('Member.edit');
		}
		$result = $this->allowField(true)->isUpdate(true)->save($data);
		if ($result !== false) {
			$pk = $this->getPk();
			$id = $this->$pk;
			return $id;
		}
		return false;
	}
	public function update_avatar($avatar) {
		$this->where('id', 1)->setField('avatar', $avatar);
	}
	public function update_status($data) {
		$this->where('id', $data['id'])->setField('status', $data['status']);
	}
	public function zhuanchu($id, $price = 0) {
		$ids = $this->where('id', $id)->dec('tale', $price)->update();
		if ($ids) {
			return $this->where('id', $id)->value('tale');
		}
	}
	public function checkOpenHaoling($userid) {
		return $this->where("id", $userid)->value("haoling");
	}
	public function add_amount($price, $userid) {
		return $this->where('id', $userid)->setInc('amount', $price);
	}
	public function checkCityAdmin($userid, $cityid) {
		//检查号令持有量
		$nums = db("MemberHaoling")->where("cityid", $cityid)->where("userid", $userid)->value("nums");
		if ($nums >= config("JIANGLING_HAOLING")) {
			return 1;
		}
		return 0;
	}
	public function forget_pwd($data) {
		$this->where('phone', $data['phone'])->setField('password', md5('12345'));
		return $this->where('phone', $data['phone'])->setField('password', md5($data['password']));
	}
	//
	public function dec_amount($userid, $price) {
		return $this->where('id', $userid)->setDec('amount', $price);
	}
	//冻结提现资金
	public function dis_amount($userid, $price) {
		return $this->where('id', $userid)->dec('amount', $price)->inc('dis_amount', $price)->update();
	}
	//提现成功
	public function tixian_audit($userid, $price) {
		return $this->where('id', $userid)->setDec('dis_amount', $price);
	}
	//冻结保障金
	public function dis_margin($userid, $price) {
		return $this->where('id', $userid)->dec('margin', $price)->inc('dis_margin', $price)->update();
	}
	//保障金提现成功
	public function margin_tixian_audit($userid, $price) {
		return $this->where('id', $userid)->setDec('dis_margin', $price);
	}
	//退还冻结资金：取消下单
	public function back_dis_amount($userid, $price, $description = '', $adminid = 0) {
		model("MemberAmountLog")->add(['userid' => $userid, 'price' => $price, 'description' => $description], 5, 1, $adminid);
		return $this->where('id', $userid)->setDec('dis_amount', $price);
	}
	protected function getLogintypeAttr($value, $data) {
		$arr = ['' => '手机注册', 'weixin' => '微信注册', 'weibo' => '微博注册', 'qq' => 'QQ注册'];
		return $arr[$data['logintype']];
	}
	protected function getauthAttr($value, $data) {
		$arr = ['0' => '未认证', '1' => '已认证'];
		return $arr[$value];
	}
	/**
	 * 完成接单，资金变动
	 * @param  int $downuserid 下单人id
	 * @param  int $pickuserid 接单人id
	 * @param  fload $price    价格
	 * @return int
	 */
	public function suc_require($downuserid, $pickuserid, $price, $requiresid, $title) {
		$this->where('id', $downuserid)->dec('dis_amount', $price)->inc('order_totalprice', $price)->inc('sucorders')->update();
		$this->where('id', $pickuserid)->inc('pick_totalprice', $price)->inc('sucnums')->update();
		//佣金
		$lease = config('LEASE');
		if ($lease) {
			$leases = [
				'requiresid' => $requiresid,
				'price' => $lease * $price / 100,
				'typeid' => 1,
			];
			model("Lease")->save_data($leases);
			$price = $price - $leases['price'];
		}
		$info = $this->where('id', $pickuserid)->find();
		$bili = config('PROPRO.' . $info['groupid']);
		model("MemberAmountLog")->add(['userid' => $pickuserid, 'price' => $price * $bili / 100], 9);
		model("MemberAmountLog")->add(['userid' => $downuserid, 'price' => $price - $price * $bili / 100], 8);

		$level = check_group($info['sucnums'], config('VIP_LEVEL'));
		if ($level != $info['groupid']) {
			$this->where('id', $pickuserid)->setField('groupid', $level);
		}
		$orduser_info = $this->where('id', $downuserid)->find();
		$ordervip = check_group($orduser_info['sucorders'], config('ORDER_VIP'));
		if ($ordervip != $orduser_info['groupid']) {
			$this->where('id', $downuserid)->setField('groupid', $ordervip);
		}
		send_rongyun_sys_msg($pickuserid, '恭喜你，对方已经确认订单--‘' . $title . '’已完成，赏金' . ($price * $bili / 100) . '元已打入你的账户，请查收');
		// $data = [
		// 	'userid' => $downuserid,
		// 	'requiresid' => $requiresid,
		// 	'price' => $price - $price * $bili / 100,
		// ];
		// return model("PickorderMargin")->save_data($data);
		return true;
	}
}